import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-03-24
 * Time: 17:04
 */
public class MyBlockingQueue {
    volatile private int[] items;
    volatile private int front;
    volatile private int rear;
    private final static int INIT = 10;

    private int size;
//获取size本身就是队列的一个方法，所以用这个不是更容易又更有用吗~
    //浪费一个下标的空间去搞，也是可以的
public MyBlockingQueue(int capacity) {
    if(capacity <= 0) {
        this.items = new int[INIT];
    }
    this.items = new int[capacity];
}

    //不可能同时阻塞~
    synchronized public void put(int value) throws InterruptedException {
        if(size == items.length) {
            this.wait();
        }
        items[rear] = value;
        rear = (rear + 1) % items.length;
        size++;
        this.notify();
    }

    synchronized public int take() throws InterruptedException {
        if(size == 0) {
            this.wait();
        }
        int ret = items[front];
        front = (front + 1) % items.length;
        size--;
        this.notify();
        return ret;
    }

    public MyBlockingQueue() {
        this.items = new int[INIT];
    }
}
